package io.renren.common.weixin.payutils;

import org.jdom.JDOMException;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by whc on 2016/3/23.
 */
@Component
public class WXJSPay {

    //统一支付接口
    private String prepayIdUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";
    private String tradeType = "JSAPI";
    private String signType = "MD5";

    //企业支付接口
    private String transfersUrl="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
    //@Value("#{app['wxpay.certPath']}")
    private String certPath;

    public Map jsApiPay(String appId,String mchId,String paternerKey,String openid, String total_fee,
                               String out_trade_num,String body,String ip,String notifyUrl) throws JDOMException, IOException {
        String noceStr=Sha1Util.getNonceStr();//随机字符串
        String timeStamp=Sha1Util.getTimeStamp();//时间戳
        // 接口package部分-内容----------------------------------------------------------
        TreeMap<String, String> contentMap = new TreeMap<String, String>();
        contentMap.put("appid", appId); // 公众账号 ID
        contentMap.put("mch_id", mchId); // 商户号
        contentMap.put("nonce_str", noceStr); // 随机字符串
        contentMap.put("body", body); // 商品描述
        contentMap.put("out_trade_no",out_trade_num); // 商户订单号
        contentMap.put("total_fee",total_fee); // 订单总金额
        contentMap.put("spbill_create_ip",ip); // 订单生成的机器IP
        contentMap.put("notify_url",notifyUrl); // 通知地址
        contentMap.put("trade_type",tradeType); // 交易类型
        contentMap.put("openid",openid); // 用户标识
        String wxpackage = WeiXinSignAndPackage.createPackage(contentMap,paternerKey);
        contentMap.put("sign", wxpackage);

        String result=WeiXinSignAndPackage.getPrepayId(contentMap,prepayIdUrl);//调用统一接口返回的值
        System.out.println("--------------------------------------------");
        System.out.println(result);
        Map<String,String> map=XMLUtil.doXMLParse(result);//调用统一接口返回的值转换为XML格式
        System.out.println(map);
        TreeMap<String, String> wxPayParamMap = new TreeMap<String, String>();
        wxPayParamMap.put("appId", appId);
        wxPayParamMap.put("timeStamp",timeStamp );
        wxPayParamMap.put("nonceStr",noceStr);
        wxPayParamMap.put("package", "prepay_id="+map.get("prepay_id"));
        wxPayParamMap.put("signType", signType);
        String paySign=WeiXinSignAndPackage.createPaySign(wxPayParamMap,paternerKey);//支付得到的签名
        wxPayParamMap.put("paySign", paySign);
        wxPayParamMap.put("payMoney", total_fee);//到前段显示使用，支付不需要此参数
        return wxPayParamMap;
    }

    /**
     * 验证签名
     * @param contentMap
     * @param sign
     * @return
     */
    public boolean vilidataSign(TreeMap<String, String> contentMap,String sign,String paternerKey){
        String wxpackage =  WeiXinSignAndPackage.createPackage(contentMap,paternerKey);
        if(sign.equals(wxpackage)){
            return true;
        }else {
            return false;
        }
    }


    //企业付款接口
    public Map transfers(String appId,String mchId,String paternerKey,String openid,
                         String partner_trade_no,String amount,String desc,String ip) throws JDOMException, IOException {

        //File file = new File();

        String noceStr = Sha1Util.getNonceStr();//随机字符串
        // 接口package部分-内容----------------------------------------------------------
        TreeMap<String, String> contentMap = new TreeMap<String, String>();
        contentMap.put("mch_appid", appId); // 公众账号 ID
        contentMap.put("mchid", mchId); // 商户号
        contentMap.put("nonce_str", noceStr); // 随机字符串
        contentMap.put("partner_trade_no", noceStr); // 商户订单号
        contentMap.put("openid", openid); // openid
        contentMap.put("check_name", "NO_CHECK"); // 校验用户姓名选项
        contentMap.put("amount", amount); // 金额
        contentMap.put("desc", desc); // 企业付款描述信息
        contentMap.put("spbill_create_ip", ip); // ip
        String sign = WeiXinSignAndPackage.createPaySign(contentMap, paternerKey);
        contentMap.put("sign", sign); //签名
        String result = WeiXinSignAndPackage.transfers(certPath, mchId, contentMap, transfersUrl);
        //String result=WeiXinSignAndPackage.transfers("d:/apiclient_cert.p12",mchId,contentMap,"https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers");
        System.out.println(result);
        Map<String, String> map = XMLUtil.doXMLParse(result);

        return map;
    }


    /*public static void main(String[] args){
        WXJSPay wxjsPay = new WXJSPay();
        String openId = "o_r1hv3LC07uzduL1NEttDFvQIuM";
        String total_fee = "1";
        String out_trade_num = "1495989355073";
        String body="test";
        String ip="127.0.0.1";
        String notifyUrl="https://api.mch.weixin.qq.com/pay/unifiedorder";
        String appId= "wxb9f088d39e1df150";
        String mchId="1480606962";
        String paternerKey="46325ebda6393e8eb0de526cc6b98398";



        try {
            wxjsPay.jsApiPay(appId,mchId,paternerKey,openId, total_fee,out_trade_num,body,ip,notifyUrl);
            //wxjsPay.transfers("wx1137883fae49db47", "1324875101", "46325ebda6393e8eb0de526cc6b98398", "oXbPmwZhruqVE72xdxBxYEeUc9P0",
            //        "20160708001","100","desc","127.0.0.1");

            *//*wxjsPay.transfers("wxaf61ebbe3aeafe54", "1334036501", "46325ebda6393e8eb0de526cc6b98399", "oj526wZu3sAkCBv9lxtTYzBOdJus",
                    "2016071415552133","100","提现","27.219.1.161");*//*
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
}
